home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJTST200.ZIP / tests / libclink / slist.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-23  |  1.6 KB  |  95 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. #include "slist.h"
  5.  
  6. StringList::StringList()
  7. {
  8.   count = 0;
  9.   max = 10;
  10.   maxc = 0;
  11.   data = (char **)malloc(10*sizeof(char *));
  12. }
  13.  
  14. StringList::~StringList()
  15. {
  16.   int i;
  17.   for (i=0; i<maxc; i++)
  18.     if (data[i])
  19.       free(data[i]);
  20.   free(data);
  21. }
  22.  
  23. char *StringList::operator[](int i)
  24. {
  25.   if (maxc > count)
  26.   {
  27.     int cc, mc;
  28.     for (cc=0, mc=0; mc<maxc; mc++)
  29.       if (data[mc])
  30.     data[cc++] = data[mc];
  31.     if (cc != count)
  32.       fprintf(stderr, "Programmer error: cc != count (%d != %d)\n", cc, count);
  33.     maxc = count;
  34.   }
  35.   if (i >= count || i < 0)
  36.     return 0;
  37.   return data[i];
  38. }
  39.  
  40. void StringList::add(char *s)
  41. {
  42.   if (has(s))
  43.     return;
  44.   if (maxc >= max)
  45.   {
  46.     max += 10;
  47.     data = (char **)realloc(data, max * sizeof(char *));
  48.   }
  49.   data[maxc] = (char *)malloc(strlen(s)+1);
  50.   strcpy(data[maxc], s);
  51.   count++;
  52.   maxc++;
  53. }
  54.  
  55. void StringList::del(char *s)
  56. {
  57.   int i;
  58.   for (i=0; i<maxc; i++)
  59.     if (data[i] && strcmp(data[i], s) == 0)
  60.     {
  61.       data[i] = 0;
  62.       count--;
  63.     }
  64. }
  65.  
  66. int StringList::has(char *s)
  67. {
  68.   int i;
  69.   for (i=0; i<maxc; i++)
  70.     if (data[i] && (strcmp(data[i], s) == 0))
  71.       return 1;
  72.   return 0;
  73. }
  74.  
  75. void StringList::flush(void)
  76. {
  77.   for (count=0; count<maxc; count++)
  78.     if (data[count])
  79.       free(data[count]);
  80.   count = maxc = 0;
  81. }
  82.  
  83. static int cmp(const void *va, const void *vb)
  84. {
  85.   char *a = *(char **)va;
  86.   char *b = *(char **)vb;
  87.   return strcmp(a, b);
  88. }
  89.  
  90. void StringList::sort(void)
  91. {
  92.   this[0];
  93.   qsort(data, count, sizeof(char *), cmp);
  94. }
  95.